<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<script>
  /*实现bind*/
  function a(params) {
    console.log(this, params);
    return {a: 120}
  }

  a.prototype.ccc = 'ccc';

  // const fn = a.bind({a: 123});

  Function.prototype.myBind = myBind;

  function myBind(target, ...args1) {
    // 原函数
    const func = this;

    // 返回值 是什么 function
    // 返回值 参数 this指向也要改变
    // 利用call/apply
    const fn = function (...args2) {
      // this指向 怎么改变
      return func.call(target, [...args1, ...args2])
    };
    fn.prototype = Object.create(func.prototype);
    fn.prototype.constructor = fn;
    return fn
  }

  const fn = a.myBind({a: 123}, 10);
  let res = fn(20);
  console.log(res);

  console.log(new fn());


</script>
</body>
</html>
